package com.example.bookreader1130.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;


import com.example.bookreader1130.Bean.CollectionItem;
import com.example.bookreader1130.R;

import java.util.List;

public class CollectionAdapter   extends ArrayAdapter<CollectionItem> {
    int resourceid ;
    public CollectionAdapter(@NonNull Context context, int resource, @NonNull List<CollectionItem> objects) {

        super(context, resource, objects);
        this .resourceid =resource ;
    }

    @Override
    public View getView(int position , View converView, ViewGroup parent){
        View view =converView ;//判断是否为空
        if (view ==null){
            view = LayoutInflater.from(getContext()).inflate(resourceid,null);


        }
        //控件实例化
        ImageView booking =view.findViewById(R.id.bookimg) ;
        TextView bookname =view.findViewById(R.id.bookname) ;
        TextView authorname =view.findViewById(R.id.authorname) ;
        ImageView iscollected =view.findViewById(R.id.iscollected) ;


        CollectionItem item =getItem(position) ;//获取数据
        booking.setImageResource(item.getBooking());
        bookname.setText(item.getBookname());
        authorname.setText( "作者： "+ item.getAuthorname());

        if ( item.isIscollected()){

            iscollected.setImageResource(R.mipmap.like);

        }else {
            iscollected.setImageResource(R.mipmap.unlike);
        }

        return view ;
    }
}
